package org.jeecg.modules.erp.goods.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.erp.goods.entity.ErpGoods;
import org.jeecg.modules.erp.goods.service.IErpGoodsPackageService;
import org.jeecg.modules.erp.goods.service.IErpGoodsService;
import org.jeecg.modules.erp.goods.vo.ErpGoodsPackageVo;
import org.jeecg.modules.erp.goods.vo.ErpGoodsPage;
import org.jeecg.modules.erp.goods.vo.ErpGoodsVo;
import org.jeecg.modules.erp.goods.vo.ErpMateralSupplierVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description: 物料基础信息
 * @Author: jeecg-boot
 * @Date: 2024-05-21
 * @Version: V1.0
 */
@Api(tags = "物料基础信息")
@RestController
@RequestMapping("/base/erpGoods")
@Slf4j
public class ErpGoodsController extends JeecgController<ErpGoods, IErpGoodsService> {

    @Autowired
    private IErpGoodsService erpGoodsService;
    @Autowired
    private IErpGoodsPackageService erpGoodsPackageService;

    /**
     * 分页列表查询
     *
     * @param erpGoods
     * @param pageNo
     * @param pageSize
     * @return
     */
    //@AutoLog(value = "物料基础信息-分页列表查询")
    @ApiOperation(value = "物料基础信息-分页列表查询", notes = "物料基础信息-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<ErpGoodsPage>> queryPageList(ErpGoods erpGoods,
                                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<ErpGoods> page = new Page<>(pageNo, pageSize);
        IPage<ErpGoodsPage> pageList = erpGoodsService.goodsPage(page, erpGoods);
        return Result.OK(pageList);
    }

    /**
     * 包含供应商
     * @param erpGoods
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "物料基础信息-分页列表查询", notes = "物料基础信息-分页列表查询")
    @GetMapping(value = "/page/goods/supplier")
    public Result<IPage<ErpGoodsPage>> pageList(ErpMateralSupplierVo erpGoods,
                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<ErpMateralSupplierVo> page = new Page<>(pageNo, pageSize);
        IPage<ErpGoodsPage> pageList = erpGoodsService.goodsSupplierPage(page, erpGoods);
        return Result.OK(pageList);
    }

    /**
     * 包含供应商
     * @param erpGoods
     * @return
     */
    @ApiOperation(value = "物料基础信息-分页列表查询", notes = "物料基础信息-分页列表查询")
    @PostMapping(value = "/list/goods/supplier")
    public Result<List<ErpGoodsPage>> goodsSupplierList(@RequestBody ErpMateralSupplierVo erpGoods) {
        List<ErpGoodsPage> list = erpGoodsService.goodsSupplierList(erpGoods);
        return Result.OK(list);
    }

    @ApiOperation(value = "物料基础信息-列表查询", notes = "物料基础信息-列表查询（根据id）")
    @PostMapping("/list/ids")
    public Result<List<ErpGoods>> queryList(@RequestBody Map<String, Object> map){
        List<String> ids = (List<String>) map.get("ids");
        List<ErpGoods> erpGoodsList = erpGoodsService.goodsByIds(ids);
        return Result.OK(erpGoodsList);
    }

    /**
     * 添加
     *
     * @param erpGoodsPage
     * @return
     */
    @AutoLog(value = "物料基础信息-添加")
    @ApiOperation(value = "物料基础信息-添加", notes = "物料基础信息-添加")
    //@RequiresPermissions("org.jeecg.modules.demo:erp_goods:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody ErpGoodsPage erpGoodsPage) {
        ErpGoods erpGoods = new ErpGoods();
        BeanUtils.copyProperties(erpGoodsPage, erpGoods);
        erpGoodsService.saveMain(erpGoods, erpGoodsPage.getErpGoodsPackageList());
        return Result.OK("添加成功！");
    }


    /**
     * 编辑
     *
     * @param erpGoodsPage
     * @return
     */
    @AutoLog(value = "物料基础信息-编辑")
    @ApiOperation(value = "物料基础信息-编辑", notes = "物料基础信息-编辑")
    //@RequiresPermissions("org.jeecg.modules.demo:erp_goods:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody ErpGoodsPage erpGoodsPage) {
        ErpGoods erpGoods = new ErpGoods();
        BeanUtils.copyProperties(erpGoodsPage, erpGoods);
        ErpGoods erpGoodsEntity = erpGoodsService.getById(erpGoods.getId());
        if(erpGoodsEntity==null) {
            return Result.error("未找到对应数据");
        }
        erpGoodsService.updateMain(erpGoods, erpGoodsPage.getErpGoodsPackageList());
        return Result.OK("编辑成功!");
    }


    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "物料基础信息-通过id删除")
    @ApiOperation(value = "物料基础信息-通过id删除", notes = "物料基础信息-通过id删除")
    //@RequiresPermissions("org.jeecg.modules.demo:erp_goods:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        erpGoodsService.delMain(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "物料基础信息-批量删除")
    @ApiOperation(value = "物料基础信息-批量删除", notes = "物料基础信息-批量删除")
    //@RequiresPermissions("org.jeecg.modules.demo:erp_goods:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.erpGoodsService.delBatchMain(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "物料基础信息-通过id查询")
    @ApiOperation(value = "物料基础信息-通过id查询", notes = "物料基础信息-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<ErpGoods> queryById(@RequestParam(name = "id", required = true) String id) {
        ErpGoods erpGoods = erpGoodsService.getById(id);
        if (erpGoods == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(erpGoods);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param erpGoods
     */
    //@RequiresPermissions("org.jeecg.modules.demo:erp_goods:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ErpGoods erpGoods) {
        return super.exportXls(request, erpGoods, ErpGoods.class, "物料基础信息");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("erp_goods:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ErpGoods.class);
    }

    /**
     * 通过id查询
     *
     * @param goodsId
     * @return
     */
    //@AutoLog(value = "产品包材关联表通过主表ID查询")
    @ApiOperation(value="产品包材关联表主表ID查询", notes="产品包材关联表-通主表ID查询")
    @GetMapping(value = "/queryErpGoodsPackageByMainId")
    public Result<List<ErpGoodsPackageVo>> queryErpGoodsPackageListByMainId(@RequestParam(name="goodsId",required=true) String goodsId) {
        List<ErpGoodsPackageVo> erpGoodsPackageList = erpGoodsPackageService.selectByMainId(goodsId);
        return Result.OK(erpGoodsPackageList);
    }

    @ApiOperation(value = "物料关联配方查询", notes = "物料关联配方查询（根据id）")
    @PostMapping("/goodsAndFormula/ids")
    public Result<List<ErpGoodsVo>> queryGoodsAndFormula(@RequestBody Map<String, Object> map){
        List<String> ids = (List<String>) map.get("ids");
        List<ErpGoodsVo> erpGoodsList = erpGoodsService.queryGoodsAndFormula(ids);
        return Result.OK(erpGoodsList);
    }

    @AutoLog(value = "物料关联配方")
    @ApiOperation(value = "物料关联配方", notes = "物料关联配方")
    @PostMapping(value = "/correlationFormula")
    public Result<String> correlationFormula(@RequestBody ErpGoods erpGoods) {
        erpGoodsService.correlationFormula(erpGoods);
        return Result.OK("关联成功！");
    }

    @ApiOperation(value = "物料关联配方查询", notes = "物料关联配方查询（根据id）")
    @GetMapping("/goodsAndFormula/ByReviewId")
    public Result<List<ErpGoodsVo>> queryByReviewId(@RequestParam(name = "id", required = true) String id){
        List<ErpGoodsVo> erpGoodsList = erpGoodsService.queryByReviewId(id);
        return Result.OK(erpGoodsList);
    }
}
